/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jaotwinker;

/**
 *
 * @author Grayson
 */
public class Database {
    java.util.ArrayList items;
    MainWindow window;
    
    public Database(MainWindow mainWindow) {
        window = mainWindow;
    }
    
    public void LoadItems(String filename) {
        
        CleanParser parser = new CleanParser();
        window.DisplayMessage("Parsing XML...");
        //window.initProgress(50000);
//        window.jProgressBar1.setStringPainted(true);
//        window.jProgressBar1.setString("Loading Items");
//        window.jProgressBar1.setIndeterminate(true);
//        window.jProgressBar1.setEnabled(true);
        //while(!window.jProgressBar1.isEnabled()) {}
        parser.parse(filename, window);
        items = (java.util.ArrayList)parser.items;
        window.DisplayMessage("Done. " + items.size() + " items.");
//        window.jProgressBar1.setIndeterminate(false);
//        window.jProgressBar1.setString("Done");
//        window.jProgressBar1.setEnabled(false);
    }
    
    public javax.swing.DefaultListModel LoadSkills() {
        java.util.List<String> skills = new java.util.ArrayList<String>();
        javax.swing.DefaultListModel model = new javax.swing.DefaultListModel();
        
        if(items.size()<=0) { return null; }
        for(int i=0;i<items.size();i++) {
            Item item = (Item)items.get(i);
            for(int j=0;j<item.Skills.size();j++) {
                Skill s = (Skill)item.Skills.get(j);
                if(!skills.contains(s.name)) { skills.add(s.name); }
            }
        }
        java.util.Collections.sort(skills);
        for(int k=0;k<skills.size();k++) {
            String sname = (String)skills.get(k);
            model.addElement(sname);
        }
        return model;
    }
    
    public void search() {
        String breed = (String)window.jComboBox1.getSelectedItem();
        String prof = (String)window.jComboBox2.getSelectedItem();
        String faction = (String)window.jComboBox3.getSelectedItem();
        int level;
        try { level=Integer.parseInt(window.jTextField1.getText()); } catch (NumberFormatException e) { return; }
        if(level>220) { level=220; window.jTextField1.setText("220"); }
        if(level<1) { level=1; window.jTextField1.setText("1"); }
        boolean SL = window.jCheckBox1.isSelected();
        boolean Full = window.jCheckBox2.isSelected();
        
        java.util.ArrayList results = new java.util.ArrayList();
        String search = window.jList1.getSelectedValue().toString();
        window.jTextField2.setText("Search for " + search + " for Level " + level + " " + faction + " " + breed + " " + prof + " " + " SL:" + SL + " AI/LE/LoX:" + Full);
         
        for(int h=0;h<window.slots.size();h++) {
            Slot slot = (Slot)window.slots.get(h);
            slot.clear();
            slot.search = search;
        } 
        
        for(int i=0;i<items.size(); i++) {
            Item item = (Item)items.get(i);
            
            boolean select = false;
            // First find the skill we are looking for
            for(int j=0;j<item.Skills.size();j++) {
                Skill s = (Skill)item.Skills.get(j);
                //System.out.println(s.name);
                if(s.name.equalsIgnoreCase(search)) { select=true; break; }
            }
            if(!select) continue;   
            
//            if (item.AOID == 274718) {
//                System.out.println("Found Item.");
//            }
            
            boolean passBreedCheck = true;
            boolean passProfCheck = true;
            boolean passFactionCheck = true;
            boolean passLvlCheck = true;
            boolean passSLCheck = true;
            boolean passPaidCheck = true;
            String profs = "", breeds = "", factions = "";

            for(int k=0;k<item.Requirements.size();k++) {
                Requirement req = (Requirement)item.Requirements.get(k);
                if(req.name.equalsIgnoreCase("Breed")) {
                    if(req.operator.equalsIgnoreCase("NOT") && req.value.equalsIgnoreCase(breed)) {
                        passBreedCheck=false; break;
                    }
                    else breeds += req.value + " ";
                }
                else if(req.name.equalsIgnoreCase("Faction") || req.name.equalsIgnoreCase("Alignment")) {
                   if(req.operator.equalsIgnoreCase("NOT") || req.operator.equalsIgnoreCase("Unequal")) {
                        if(req.value.equalsIgnoreCase(faction)) {
                            passFactionCheck=false; break;
                        }
                    }
                    else factions += req.value + " ";
                }
                else if(req.name.equalsIgnoreCase("Profession") || req.name.equalsIgnoreCase("VisualProfession")) {
                   if(req.operator.equalsIgnoreCase("NOT") && req.value.equalsIgnoreCase(prof)) {
                        passProfCheck=false; break;
                    }
                   else profs += req.value + " ";
                }
                else if(req.name.equalsIgnoreCase("Level")) {
                    int lvlReq = Integer.parseInt(req.value);
                    if(req.operator.equalsIgnoreCase("GreaterThan")) lvlReq++;
                    if(level<lvlReq) { passLvlCheck=false; break; }
                }
                else if(req.name.equalsIgnoreCase("TitleLevel")) {
                    int tl = Integer.parseInt(req.value);
                    if(req.operator.equalsIgnoreCase("GreaterThan")) tl++;
                    if(tl>=7 && level<205) { passLvlCheck=false; break; }
                    if(tl>=6 && level<190) { passLvlCheck=false; break; }
                    if(tl>=5 && level<150) { passLvlCheck=false; break; }
                    if(tl>=4 && level<100) { passLvlCheck=false; break; }
                    if(tl>=3 && level<50) { passLvlCheck=false; break; }
                    if(tl>=2 && level<15) { passLvlCheck=false; break; }
                    if(tl>=1 && level<1) { passLvlCheck=false; break; }
                }

                // SL
                if(!SL) {
                    if(req.name.equalsIgnoreCase("Expansion")) {
                        if(req.value.equalsIgnoreCase("Shadow Lands")) { passSLCheck=false; break; }
                    }
                }
                // Full paid
                if(!Full) {
                    if(req.name.equalsIgnoreCase("Expansion")) {
                        if(req.value.equalsIgnoreCase("Alien Invasion")) { passPaidCheck=false; break; }
                        if(req.value.equalsIgnoreCase("Lost Eden")) { passPaidCheck=false; break; }
                        if(req.value.equalsIgnoreCase("Legacy of Xan")) { passPaidCheck=false; break; }
                    }
                }
            }
            
            if(!profs.contains(prof) && profs.length()>0) passProfCheck=false;
            if(!factions.contains(faction) && factions.length()>0) passFactionCheck=false;
            if(!breeds.contains(breed) && breeds.length()>0) passBreedCheck=false;

            if ((passBreedCheck && passProfCheck && passFactionCheck && passLvlCheck && passSLCheck && passPaidCheck) || item.Requirements.size()<=0) {
                for(int l=0;l<window.slots.size();l++) {
                    Slot slot = (Slot)window.slots.get(l);
                    if(item.slot.contains(slot.name)) {
                        if(slot.tab.equalsIgnoreCase("Weapon")) {
                            if(item.ItemType.equalsIgnoreCase("Weapon") || item.ItemType.equalsIgnoreCase("Utility")) {
                                slot.items.add(item);
                            }
                        }
                        else if(slot.tab.equalsIgnoreCase("Armor")) {
                            if(item.ItemType.equalsIgnoreCase("Armor")) {
                                slot.items.add(item);
                            }
                        }
                        else if(slot.tab.equalsIgnoreCase("Implant")) {
                            if(item.ItemType.equalsIgnoreCase("Implant") && !prof.equalsIgnoreCase("Shade")) {
                                slot.items.add(item);
                            }
                            else if(item.ItemType.equalsIgnoreCase("Spirit") && prof.equalsIgnoreCase("Shade")) {
                                slot.items.add(item);
                            }
                        }
                        
                    }
                }
                
            }
        }
        
        for(int h=0;h<window.slots.size();h++) {
            Slot slot = (Slot)window.slots.get(h);
            if(slot.items.size()>=1) {
                slot.sort(search);
                slot.selectItem(0);
                if(window.compressImplants) slot.shrinkImplants();
            }
        }
    }
}
